﻿
using System;
using System.Collections.Generic;
namespace HBase {
    public class TaskPool {
        private static Dictionary<String ,ITask> taskDictionary = new Dictionary<string ,ITask>();
        static public List<int> ConnIps = new List<int>();
        static public List<int> Jobs = new List<int>();
        static public List<int> Synchors = new List<int>();
        static public bool checkSuperiorIndex(ZtTask t) {
            if (t is ConnIp) {
                if (ConnIps.Contains(t.getSuperiorIndex())) {
                    return false;
                }
                ConnIps.Add(t.getSuperiorIndex());
            } else if (t is Job) {
                if (Jobs.Contains(t.getSuperiorIndex())) {
                    return false;
                }
                Jobs.Add(t.getSuperiorIndex());
            } else if (t is Synchor) {
                if (Synchors.Contains(t.getSuperiorIndex())) {
                    return false;
                }
                Synchors.Add(t.getSuperiorIndex());
            }

            return true;
        }


        public static void add(ZtTask task ,String key = null) {
            if( task == null ) {
                throw new Exception(key + "新线程为空");
            }
            String name = task.GetType().Name;
            if( !String.IsNullOrEmpty(key) ) {
                name += "_" + key;
            }
            if( taskDictionary.ContainsKey(name) ) {
                throw new Exception(key + "已存在");
            }
            taskDictionary[name] = task;
        }
        public static ITask get(String key) {
            if( taskDictionary.ContainsKey(key) ) {
                return taskDictionary[key];
            } else {
                return null;
            }
        }
        public static T get<T>(int id) where T : ITask {
            Type type = typeof(T);
            String name = type.Name;
            name += id;
            ITask task = get(name);
            if( task == null ) {
                return default;
            }
            return ( T ) task;
        }
        public static T getCanNull<T>(int id) where T : ITask {
            Type type = typeof(T);
            String name = type.Name;
            name += id;
            ITask task = get(name);
            if( task == null ) {
                return default(T);
            }
            return ( T ) task;
        }
        public static T get<T>() where T : ITask {
            Type type = typeof(T);
            String name = type.Name;
            ITask task = get(name);
            if( task == null ) {
                throw new Exception(name + "不存在");
            }
            return ( T ) task;
        }
        public static void doWork<T>() where T : ZtTask {
            T t = get<T>();
            t.work();

        }

        /***
        public static ZtTask get(Type type) {
            String name=type.Name;
            ZtTask task =  get(name);
            if(task == null) {
                throw new Exception(name + "不存在");
            }
            return task;
        }
        **/
        public static void start(params Type[] keys) {
            foreach( Type key in keys ) {
                start(key.Name);
            }
        }
        private static void start(params String[] keys) {
            foreach( String key in keys ) {
                ITask task = get(key);
                if( task == null ) {
                    LoggerCommon.startError(InfoCode.start_emply, key + "任务未初始化",null);
                } else {
                    task.start();
                }
            }
        }
        public static List<TaskStat> getStat() {
            List<TaskStat> l = new List<TaskStat>();
            foreach( var k in taskDictionary ) {
                l.Add(k.Value.getStat());
            }
            return l;
        }
        public static List<ITask> getZtTask() {
            List<ITask> l = new List<ITask>();
            foreach( var k in taskDictionary ) {
                l.Add(k.Value);
            }
            return l;
        }

        public static List<ITask> setInit() {
            List<ITask> l = new List<ITask>();
            foreach (var k in taskDictionary) {
               (( ZtTask) k.Value).runed=false;
            }
            return l;
        }

        public static ITask getIniting() {
            List<ZtTask> l = new List<ZtTask>();
            foreach( var k in taskDictionary ) {
                if( !k.Value.isRuned() ) {
                    return k.Value;
                }
            }
            return null;
        }


      
    }
}
